<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Location extends AdminBase {
	public function __construct()
    {
        parent::__construct();
    }

	public function index()
	{
		$post = $this->input->post();
		if($post){
			if(isset($post['id'])){//name order 
				if(isset($post['name'])){//修改名称
					if(mb_strlen($post['name'],"UTF8")>20 || mb_strlen($post['name'],"UTF8")<1){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'语言名称为2-8个字符！')));
						return;
					}
					$this->db->where('id',$post['id']);
					$this->db->update('location',$post);
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'ok','message'=>'修改成功！')));
					return;
				}else{
					if(isset($post['order'])){//排序
						$this->db->where('id',$post['id']);
						$this->db->update('location',['order'=>$post['order']]);
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'ok','message'=>'排序成功！')));
						return;
					}else{//删除  检查是否有属于该语言的商品
						$product = $this->db->get_where('product',['location_id'=>$post['id']])->row_array();
						if($product){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'删除失败，请先修改或删除属于该地区的商品！')));
							return;
						}elseif($this->db->count_all('location')==1){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'删除失败，至少保留一种货币！')));
							return;
						}else{
							$this->db->delete('location', array('id' => $post['id']));
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'ok','message'=>'删除成功！')));
							return;
						}
					}
				}
			}else{ //新增
				if(mb_strlen($post['name'],"UTF8")>20 || mb_strlen($post['name'],"UTF8")<1){
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'failed','message'=>'地区名称为1-20个字符！')));
					return;
				}
				$this->db->select('order');
				$this->db->order_by('order', 'DESC');
				$max = $this->db->get('location')->row_array();
				$post['order'] = $max?$max['order']+1:0;
				$this->db->insert('location',$post);
				$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'ok','message'=>'新增成功！')));
				return;
			}
		}else{
			$this->db->order_by('order','ASC');
			$this->db->select('location.id,location.name,location.order,currency.id as currency_id,currency.name as currency,lang.name as lang,lang.id as lang_id');
			$this->db->join('currency', 'location.currency_id = currency.id');
			$this->db->join('lang', 'location.lang_id = lang.id');
	        $location = $this->db->get('location')->result_array();
	        $lang = $this->db->select('id,name')->get('lang')->result_array();
	        $currency = $this->db->select('id,name')->get('currency')->result_array();
			$this->admin_view('location_list','地区管理',[
				'list'=>$location,
				'lang_option'=>json_encode($lang,true),
				'currency_option'=>json_encode($currency,true)
			]);
		}


        
	}
}
